<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\AdminUser;
use app\admin\model\AuthGroup;
use app\admin\model\AuthRule;

class Hsadmin extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->assign('auth_group_list',AuthGroup::all());
    }

    public function index()
    {
        $list = AdminUser::all(['id'=>['<>',1]]);
        return $this->fetch('index', ['admin_user_list' => $list]);
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function save()
    {
        if ($this->request->isAjax()) {
            $m = new AdminUser;
            return $m->_save();
        }
    }

    public function edit($id)
    {
        $admin_user = AdminUser::get($id);
        return $this->fetch('edit', ['admin_user' => $admin_user]);
    }

    public function update()
    {
        if ($this->request->isAjax()) {
            $m = new AdminUser;
            return $m->_update();
        }
    }
    //软删除
    public function delete($id)
    {
        if($this->request->isAjax()){
            if ($id == 1) $this->error('默认管理员不可删除');
            $m = new AdminUser();
            return $m->_delete($id);
        }
    }
    //角色组
    public function group_index()
    {
        return $this->fetch('group_index', ['auth_group_list' => AuthGroup::all()]);
    }
    /*添加角色组*/
    public function group_add()
    {
        return $this->fetch('group_add');
    }
    //保存权限组
    public function group_save()
    {
        if ($this->request->isAjax()) {
            $m = new AuthGroup;
            return $m->_save();
        }
    }
    /*编辑权限组*/
    public function group_edit($id)
    {
        return $this->fetch('group_edit', ['auth_group' => AuthGroup::get($id)]);
    }
    //更新权限组
    public function group_update()
    {
        if ($this->request->isAjax()) {
            $m = new AuthGroup;
            return $m->_update();
        }
    }
    //删除权限组
    public function group_delete($id)
    {
        if($this->request->isAjax()){
            if ($id == 1) $this->error('超级管理组不可删除');
            $m = new AuthGroup;
            return $m->_delete($id);
        }
    }
    /*授权*/
    public function auth($id)
    {
        return $this->fetch('auth', ['id' => $id]);
    }

    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getJson($id)
    {
        if($this->request->isAjax()){
            $m = new AuthRule();
            $auth_group_data = AuthGroup::get($id);
            $auth_rules      = explode(',', $auth_group_data['rules']);
            $auth_rule_list  = $m->field('id,pid,title')->order('sort desc,id')->select();
            foreach ($auth_rule_list as $key => $value) {
                in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
            }
            return $auth_rule_list;
        }
    }
    /**
     * 更新权限组规则
     * @return array
     */
    public function updateAuthGroupRule()
    {
        if ($this->request->isAjax()) {
            $m = new AuthGroup;
            return $m->_updateAuthGroupRule();
        }
    }
}